<?php

class Wide extends TTemplateControl
{
	/**
	 * Layout Constructor.
	 * 
	 * Calls the parent constructor.
	 */
	public function __construct()
	{
		parent::__construct();
	}

	/**
	 * Set the TMultiView for logged-in or not.
	 * 
	 * This changes the login state in the welcome div.
	 * @param mixed $param I've no idea what this actually is.
	 */
	public function onLoad ($param)
	{
		$authManager=$this->Application->getModule('auth');
		$logged_in = !$authManager->User->IsGuest;
		
		$this->menuBar($logged_in);
	}
	
	/**
	 * Gets the application context (the relative URL for the prado app without the index.php bit).
	 */
	public function getApplicationContext()
	{
		$context = $this->Application->Request->ApplicationUrl;
		return substr_replace($context,'',-9);
	}
	/**
	 * Create the MenuBar.
	 * 
	 * Parse the $pageService to see what page we're on.  Then use that info
	 * and the $logged_in state to create the <li>'s for the MenuBar.
	 * 
	 * So, if we're logged in and on the Home page, the MenuBar will hold all
	 * the items from $MENUBAR_ITEMS_USER except the one with RequestedPagePath
	 * Home.
	 * 
	 * @param bool $logged_in guest or user state?
	 * @staticvar array $MENUBAR_ITEMS_GUEST An array of  prado_path => descriptive_title for guest options.
	 * @staticvar array $MENUBAR_ITEMS_USER An array of prado_path => descriptive_title for user options. 
	 */
	public function menuBar($logged_in)
	{
		$MENUBAR_ITEMS_GUEST = array(
			$this->Application->Request->ApplicationUrl . '?page=Home' => 'Home',
			$this->Application->Request->ApplicationUrl . '?page=About' => 'About Tiro',
			$this->Application->Request->ApplicationUrl . '?page=UserManagement.Login' => 'Login',
			$this->Application->Request->ApplicationUrl . '?page=UserManagement.Create' => 'Create User'
			);
		$MENUBAR_ITEMS_USER = array(
			$this->Application->Request->ApplicationUrl . '?page=Home' => 'Home',
			$this->Application->Request->ApplicationUrl . '?page=About' =>'About Tiro',
			$this->Application->Request->ApplicationUrl . '?page=UserManagement.Logout' => 'Logout'
			);
		
		if(!$logged_in)
		{
			$this->MenuList->DataSource=$MENUBAR_ITEMS_GUEST;
			$this->MenuList->databind();
		}
		else
		{
			$this->MenuList->DataSource=$MENUBAR_ITEMS_USER;
			$this->MenuList->databind();
		}
	}
	
	public function logoutButtonClicked()
	{
		$this->Application->getModule('auth')->logout();
		$url=$this->Service->DefaultPageUrl;
		
		$this->Response->redirect($url);
	}
}
?>